<?php
	class ThumbcutAction extends CommonAction{
		public function uploadthumb(){
			if (isset($_SESSION['user_file_large_path'])){
				$current_large_image_width = self::getWidth('.'.$_SESSION['user_file_large_path']);
				$current_large_image_height = self::getHeight('.'.$_SESSION['user_file_large_path']);
			}
			
			if (isset($_SESSION['user_file_large_path']) && isset($_SESSION['the_end_thumb_cut']) && file_exists('.'.$_SESSION['user_file_large_path']) && file_exists('.'.$_SESSION['the_end_thumb_cut'])){
				$this->display("thumb_post");
			}else{
				if(isset($_SESSION['user_file_large_path']) && file_exists('.'.$_SESSION['user_file_large_path'])){
					$this->assign("current_large_image_width",$current_large_image_width);
					$this->assign("current_large_image_height",$current_large_image_height);
					$this->display("thumb_cut");
				}else{
					$this->display("thumb_up");
				}
			}
		}

		public function thumbcut(){
			if ( isset( $_REQUEST['custom'] ) && $_REQUEST['custom'] == 'video' ){
				$custom_width = 460;
				$custom_height = 320;
			}else{
				$custom_width = C('PIC_THUMB_WIDTH');
				$custom_height = C('PIC_THUMB_HEIGHT');
			}
			//
			if ($_POST['upload_cancel']){
				$file = '.'.$_SESSION['user_file_large_path'];
				if (file_exists($file)){
					unlink($file);
				}
				unset($_SESSION['user_file_foder']);
				unset($_SESSION['user_file_name']);
				unset($_SESSION['user_file_name_noext']);
				unset($_SESSION['user_file_ext']);
				unset($_SESSION['user_file_large_path']);
				unset($_SESSION['user_file_thumb_path']);
				if ( isset( $_REQUEST['custom'] ) && $_REQUEST['custom'] == 'video' ){
					$this->redirect("thumbcut/uploadthumb",array('custom'=>'video'));
				}else{
					$this->redirect("thumbcut/uploadthumb");
				}
			}
			if ($_POST["upload_thumbnail"]) {
				$filearray = explode('/',$_SESSION['user_file_thumb_path']);
				$finenew = $filearray[count($filearray) - 1];
				$newpath = '';
				for ($i = 1; $i<=count($filearray)-2; $i++){
					$newpath .= '/'.$filearray[$i];
				}
				$endfile = $newpath.'/thumb_'.$finenew;
				$x1 = $_POST["x1"];
				$y1 = $_POST["y1"];
				$x2 = $_POST["x2"];
				$y2 = $_POST["y2"];
				$w = $_POST["w"];
				$h = $_POST["h"];
				$scale = $custom_width/$w;
				$cropped = $this->resizeThumbnailImage('.'.$endfile, '.'.$_SESSION['user_file_large_path'],$w,$h,$x1,$y1,$scale);
				$_SESSION['the_end_thumb_cut'] = $endfile;
				//mini裁切
				import("ORG.Net.Thumb");
				$thumb = new ThumbHandler();
				
				$thumb->setSrcImg($cropped, 0, 0);		
				$thumb->setDstImg('.'.$newpath.'/mini_'.$finenew, 0, "#000000");		
				$thumb->setDstImgSize(1, C("PIC_MINI_WIDTH"), C("PIC_MINI_HEIGHT"));		
				$thumb->createImg(95, 0);
				$_SESSION['the_end_mini_cut'] = $newpath.'/mini_'.$finenew;
				
				if ( isset( $_REQUEST['custom'] ) && $_REQUEST['custom'] == 'video' ){
					$this->redirect("thumbcut/uploadthumb",array('custom'=>'video'));
				}else{
					$this->redirect("thumbcut/uploadthumb");
				}
			}
		}
		
		public function uploadpost(){
			import("ORG.Net.UploadFile");
			$upload = new UploadFile();
			$uploadfilepath = __ROOT__.'/'.C('WEB_PUBLIC_PATH').'/'.C('DIR_ATTCH_PATH');
			$upload->savePath = ".".$uploadfilepath."/";
			$upload->maxSize = 3145728;
			$upload->allowExts = array('jpg','gif','png','jpeg');
			$upload->saveRule = 'uniqid';
			$upload->autoSub = true;
			$upload->subType = 'date';
			$upload->dateFormat = "Ymd";
			if(!$upload->upload()){
				$error = $this->error($upload->getErrorMsg());
			}else{
				$info = $upload->getUploadFileInfo();
				$user_file_name_noext = explode('/',$info[0]['savename']);
				$old_name = explode('.',$user_file_name_noext[1]);
				$file_l_path = trim($info[0]['savepath'],'..').$info[0]['savename'];
				$_SESSION['user_file_foder'] = date("Ymd",time());
				$_SESSION['user_file_name'] = $user_file_name_noext[1];
				$_SESSION['user_file_ext'] = $info[0]['extension'];
				$_SESSION['user_file_name_noext'] = $old_name[0];
				$_SESSION['user_file_large_path'] = $file_l_path;
				$_SESSION['user_file_thumb_path'] = $file_l_path;
			}
			
			if ( isset( $_REQUEST['custom'] ) && $_REQUEST['custom'] == 'video' ){
				$this->redirect("thumbcut/uploadthumb",array('custom'=>'video'));
			}else{
				$this->redirect("thumbcut/uploadthumb");
			}
		}

		public function thumbdelete(){
			if (isset($_SESSION['the_end_thumb_cut'])){
	    		unlink('.'.$_SESSION['the_end_thumb_cut']);
	    		unset($_SESSION['the_end_thumb_cut']);
	    	}
			if (isset($_SESSION['the_end_mini_cut'])){
				unlink('.'.$_SESSION['the_end_mini_cut']);
				unset($_SESSION['the_end_mini_cut']);
			}
	    	if ( isset( $_REQUEST['custom'] ) && $_REQUEST['custom'] == 'video' ){
				$this->redirect("thumbcut/uploadthumb",array('custom'=>'video'));
			}else{
				$this->redirect("thumbcut/uploadthumb");
			}
		}
		
		public function alldelete(){
			if (file_exists('.'.$_SESSION['user_file_large_path'])){
				unlink('.'.$_SESSION['user_file_large_path']);
			}
			if (file_exists('.'.$_SESSION['the_end_thumb_cut'])){
				unlink('.'.$_SESSION['the_end_thumb_cut']);
			}
			if (file_exists('.'.$_SESSION['the_end_mini_cut'])){
				unlink('.'.$_SESSION['the_end_mini_cut']);
			}
			unset($_SESSION['user_file_foder']);
			unset($_SESSION['user_file_name']);
			unset($_SESSION['user_file_name_noext']);
			unset($_SESSION['user_file_ext']);
			unset($_SESSION['user_file_large_path']);
			unset($_SESSION['user_file_thumb_path']);
			unset($_SESSION['the_end_thumb_cut']);
			unset($_SESSION['the_end_mini_cut']);
			if ( isset( $_REQUEST['custom'] ) && $_REQUEST['custom'] == 'video' ){
				$this->redirect("thumbcut/uploadthumb",array('custom'=>'video'));
			}else{
				$this->redirect("thumbcut/uploadthumb");
			}
		}
	
		private function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height, $scale){
			list($imagewidth, $imageheight, $imageType) = getimagesize($image);
			$imageType = image_type_to_mime_type($imageType);
			
			$newImageWidth = ceil($width * $scale);
			$newImageHeight = ceil($height * $scale);
			$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
			switch($imageType) {
				case "image/gif":
					$source=imagecreatefromgif($image); 
					break;
			    case "image/pjpeg":
				case "image/jpeg":
				case "image/jpg":
					$source=imagecreatefromjpeg($image); 
					break;
			    case "image/png":
				case "image/x-png":
					$source=imagecreatefrompng($image); 
					break;
		  	}
			imagecopyresampled($newImage,$source,0,0,$start_width,$start_height,$newImageWidth,$newImageHeight,$width,$height);
			switch($imageType) {
				case "image/gif":
			  		imagegif($newImage,$thumb_image_name); 
					break;
		      	case "image/pjpeg":
				case "image/jpeg":
				case "image/jpg":
			  		imagejpeg($newImage,$thumb_image_name,90); 
					break;
				case "image/png":
				case "image/x-png":
					imagepng($newImage,$thumb_image_name);  
					break;
		    }
			chmod($thumb_image_name, 0777);
			return $thumb_image_name;
		}
		
		private function getHeight($image) {
			$size = getimagesize($image);
			$height = $size[1];
			return $height;
		}
		
		private function getWidth($image) {
			$size = getimagesize($image);
			$width = $size[0];
			return $width;
		}
	}
?>